﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Test1();
            Test2();
            Test3();
            Test4();
            Test5();
            Test6();
            Test7();
            Test8();
            Test9();
            Test10();  
        }
        //foreach统计字符
        static void Test1()
        {
            Console.WriteLine("请输入字符串:");
            string str = Convert.ToString(Console.ReadLine());
            int LetterCount = 0;
            int DigitCount = 0;
            int SpaceCount = 0;
            foreach (var s in str)
            {
                if (char.IsLetter(s))
                {
                    LetterCount++;
                }
                else if (char.IsDigit(s))
                {
                    DigitCount++;
                }
                else if (char.IsWhiteSpace(s))
                {
                    SpaceCount++;
                }
            }
            Console.WriteLine("您输入的字符串中字母有{0}个，数字有{1}个，空格有{2}个", LetterCount, DigitCount, SpaceCount);
        }
        //统计和和平均值
        static void Test2()
        {
            double[] arr = new double[5];
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine("请输入第" + (i + 1) + "位学生成绩:");
                arr[i] = double.Parse(Console.ReadLine());
            }
            double sum = 0;
            double avg = 0;
            foreach (double num in arr)
            {
                sum += num;
                avg = sum / 5;
            }
            Console.WriteLine("这五名学生的总成绩为：{0},平均成绩为：{1}", sum, avg);
        }
        //数组排序
        static void Test3()
        {
            int[] arr = { 10, 20, 30, 40, 50 };
            Console.WriteLine("交换前：");
            foreach (int item in arr)
            {
                Console.Write(item + " ");
            }
            int temp = 0;
            for (int i = 0; i < arr.Length; i++)
            {
                for (int j = 0; j < arr.Length - i - 1; j++)
                {
                    if (arr[j] < arr[j + 1])
                    {
                        temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
                }
            }
            Console.WriteLine();
            Console.WriteLine("交换后：");
            foreach (int item in arr)
            {
                Console.Write(item + " ");
            }
        }
        //找数组元素
        static void Test4()
        {
            int[] arr = { 10, 20, 30, 40, 50, 10 };
            foreach (int item in arr)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine("请输入一个数：");
            int sum = int.Parse(Console.ReadLine());
            int index = 0;
            for (int i = arr.Length - 1; i >= 0; i--)
            {
                if (sum == arr[i])
                {
                    index = i;
                    Console.WriteLine("{0}在数组中最后出现的位置是：{1}", sum, index);
                    break;
                }
                if (i == 0 && sum != arr[i])
                {
                    Console.WriteLine("在数组中找不到{0}这个数", sum);
                }
            }
        }
        //打印九九乘法表
        static void Test5()
        {
            for (int i = 1; i < 10; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write("{0}*{1}=" + (i * j) + " ", i, j);
                }
                Console.WriteLine();
            }
        }
        //打印空心正方形
        static void Test6()
        {
            Console.WriteLine("输入正方形边长：");
            int a = int.Parse(Console.ReadLine());
            for (int i = 0; i < a; i++)
            {
                for (int j = 0; j < a; j++)
                {
                    if (i == 0 || i == a - 1 || j == 0 || j == a - 1)
                    {
                        Console.Write("*");
                    }
                    else
                    {
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }
        }
        //打印菱形
        static void Test7()
        {
            for (int i = 1; i <= 3; i++)
            {
                for (int j = 1; j <= 3 - i; j++)
                {
                    Console.Write(" ");
                }
                for (int k = 1; k <= i * 2 - 1; k++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }
            for (int i = 1; i <= 3; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write(" ");
                }
                for (int k = 1; k <= 5 - 2 * i; k++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
        //打印实心正方形
        static void Test8()
        {
            Console.WriteLine("请输入正方形边长：");
            int a = int.Parse(Console.ReadLine());
            for (int i = 1; i <= a; i++)
            {
                for (int j = 1; j <= a; j++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }
        }
        //下标偶数
        static void Test9()
        {
            string[] arr = new string[5];
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine("请输入一个字符串");
                arr[i] = Convert.ToString(Console.ReadLine());
            }
            Console.WriteLine("数组里的数为：");
            foreach (string item in arr)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine("数组里下标为偶数的值为：");
            for (int i = 0; i < arr.Length; i = i + 2)
            {
                Console.WriteLine(arr[i]);
            }
        }
        //数组排序
        static void Test10()
        {
            int[] arr = { 90, 80, 70, 60, 50 };
            int[] arr1 = new int[6];
            Console.WriteLine("请输入要插入的数");
            int a = int.Parse(Console.ReadLine());
            int index = -1;
            for (int i = 0; i < arr.Length; i++)
            {
                if (a > arr[i])
                {
                    index = i;
                    break;
                }
                if (a < arr[arr.Length - 1])
                {
                    index = arr.Length;
                }
            }
            Console.WriteLine(index);
            for (int i = 0; i < arr1.Length; i++)
            {
                if (i < index)
                {
                    arr1[i] = arr[i];
                }
                if (i == index)
                {
                    arr1[i] = a;
                }
                if (i > index)
                {
                    arr1[i] = arr[(i - 1)];
                }
            }
            Console.Write("更改后的数组：");
            foreach (int num in arr1)
            {
                Console.Write(num + " ");
            }
        }
    }
}
